<!--
 * @Author: Li Zengkun
 * @Date: 2022-09-28 20:23:07
 * @LastEditors: Li Zengkun
 * @LastEditTime: 2022-09-28 21:26:35
 * @Description: 
-->
<template>
  <div>
    <el-card>
      <template #header>
        <div style="display: flex; justify-content: space-between">
          <span>管理员角色</span>
          <el-button icon="Plus" type="success" @click="AddDialogShow = true"
            >添加</el-button
          >
        </div>
      </template>
      <el-table :data="roleList">
        <el-table-column label="#" prop="id"></el-table-column>
        <el-table-column label="分类">
          <template #default="scope">
            <el-tag>{{ scope.row.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button
              :disabled="scope.row.id === 1"
              icon="Edit"
              @click="openEditDialog(scope.row)"
              type="primary"
              >编辑</el-button
            >
            <el-button
              :disabled="scope.row.id === 1"
              icon="Delete"
              @click="handleDelete(scope.row, scope.$index)"
              type="danger"
              >删除</el-button
            >
            <el-button icon="Setting" @click="handleSetting" type="success"
              >设置</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 编辑 对话框 -->
    <el-dialog title="编辑角色" v-model="EditDialogShow">
      <el-form ref="editFormRef" :model="needEditForm" :rules="editRules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="needEditForm.name"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="EditDialogShow = false">取消</el-button>
          <el-button type="primary" @click="handleEdit">保存</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 添加 对话框 -->
    <el-dialog title="添加角色" v-model="AddDialogShow">
      <el-form ref="addFormRef" :model="needAddForm" :rules="addRules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="needAddForm.name"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="AddDialogShow = false">取消</el-button>
          <el-button type="primary" @click="handleAdd">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { ref, reactive } from "vue";
import { role } from "@/api";

const resp = await role.getRoleList();
const roleList = ref(resp.data);
console.log(resp.data);

// 添加
const addFormRef = ref(null);
const needAddForm = ref({ name: "" });
const AddDialogShow = ref(false);
const addRules = reactive({
  name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
});
const handleAdd = async () => {
  addFormRef.value.validate(async (valid) => {
    if (valid) {
      let { status, msg, data } = await role.addRole(needAddForm.value);
      if (status) {
        roleList.value.push({ ...data, ...needAddForm.value });
        needAddForm.value.name = "";
      }
      ElMessage({
        type: status ? "success" : "error",
        message: msg,
      });
      AddDialogShow.value = false;
    }
  });
};

// 编辑
const editFormRef = ref(null);
const EditDialogShow = ref(false);
const needEditForm = ref({});
const editRules = reactive({
  name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
});
const openEditDialog = (row) => {
  needEditForm.value = row;
  EditDialogShow.value = true;
};
const handleEdit = () => {
  editFormRef.value.validate(async (valid) => {
    if (valid) {
      let { status, msg } = await role.update(needEditForm.value.id, {
        ...needEditForm.value,
      });
      if (status) {
        needEditForm.value = {};
      }
      ElMessage({
        type: status ? "success" : "error",
        message: msg,
      });
      EditDialogShow.value = false;
    }
  });
};

// 删除
const handleDelete = (row, index) => {
  console.log(index);
  ElMessageBox.confirm(`是否要删除 ${row.name}`, "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let { status, msg } = await role.removeRole(row.id);
      if (status) {
        roleList.value.splice(index, 1);
      }
      ElMessage({
        type: status ? "success" : "error",
        message: msg,
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消",
      });
    });
};
</script>

<style lang='less' scoped>
</style>
